package com.loan.controller.app.data;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.loan.common.annotation.Log;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.Result;
import com.loan.common.core.domain.entity.SysDept;
import com.loan.common.core.domain.entity.SysUser;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.common.utils.poi.ExcelUtil;
import com.loan.system.domain.*;
import com.loan.system.domain.vo.LastOverDataExportVo;
import com.loan.system.domain.vo.OneOverDataExportVo;
import com.loan.system.domain.vo.SysUserExportVo;
import com.loan.system.domain.vo.TwoOverDataExportVo;
import com.loan.system.service.*;
import com.loan.system.service.impl.OverOneDataServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author shiguang on 2022/10/8 10:10
 */
@Validated
@Api(value = "数据统计", tags = {"数据统计"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app/data")
public class DataController extends BaseController {

    private final ApplyService applyService;

    private final ExamineDataService examineDataService;

    private final ExamineOverDataService examineOverDataService;

    private final OverOneDataService overOneDataService;

    private final OverTwoDataService overTwoDataService;

    private final OverLastDataService overLastDataService;

    private final CompanyDataService companyDataService;

    private final WithdrawalDataService WithdrawalDataService;

    /**
     * 本金收回率
     */
    @ApiOperation("本金收回率（分页）")
    @GetMapping("/capital/page")
    public Result<List<Map<String, Object>>> list(String beginTime, String endTime) {
        return Result.ok(applyService.selectPageCapitalBack(beginTime, endTime));
    }

    @ApiOperation("商户报表")
    @GetMapping("/companyData/page")
    public TableDataInfo<CompanyDataEntity> companyDataList(String beginTime, String endTime, Long companyId, PageQuery pageQuery) {
        return companyDataService.selectCompanyDataList(beginTime, endTime, companyId, pageQuery);
    }

    @ApiOperation("提现报表")
    @GetMapping("/payData/page")
    public TableDataInfo<Map<String, Object>> payDataList(String beginTime, String endTime, Long customerId, PageQuery pageQuery) {
        return applyService.selectPayDataList(beginTime, endTime, customerId, pageQuery);
    }

    @ApiOperation("提现监督报表")
    @GetMapping("/withdrawal/page")
    public TableDataInfo<WithdrawalDataEntity> withdrawalList(String beginTime, String endTime, Long companyId, PageQuery pageQuery) {
        return WithdrawalDataService.withdrawalList(beginTime, endTime, companyId, pageQuery);
    }

    @ApiOperation("提现报表汇总")
    @GetMapping("/payData/total")
    public Result<Map<String, Object>> payDataTotal(String beginTime, String endTime, Long customerId) {
        return Result.ok(applyService.selectPayDataTotal(beginTime, endTime, customerId));
    }


    @ApiOperation("审核信息统计")
    @GetMapping("/examineData/page")
    public TableDataInfo<ExamineDataEntity> examineDataList(String beginTime, String endTime, Long oneExamineBy, PageQuery pageQuery) {
        return examineDataService.selectPageExamineDataList(beginTime, endTime, oneExamineBy, pageQuery);
    }

    @ApiOperation("审核逾期统计")
    @GetMapping("/examineOver/page")
    public TableDataInfo<ExamineOverDataEntity> examineOverDataList(String beginTime, String endTime, String type, PageQuery pageQuery) {
        return examineOverDataService.selectPageExamineOverDataList(beginTime, endTime, type, pageQuery);
    }

    @ApiOperation("s0催收统计")
    @GetMapping("/overOneData/page")
    public TableDataInfo<OverOneDataEntity> overOneDataList(String beginTime, String endTime, Long userId, PageQuery pageQuery) {
        return overOneDataService.selectPageOverOneDataList(beginTime, endTime, userId, pageQuery);
    }

    @ApiOperation("s1催收统计")
    @GetMapping("/overTwoData/page")
    public TableDataInfo<OverTwoDataEntity> overTwoDataList(String beginTime, String endTime, Long userId, PageQuery pageQuery) {
        return overTwoDataService.selectPageOverTwoDataList(beginTime, endTime, userId, pageQuery);
    }

    @ApiOperation("s2,s3催收统计")
    @GetMapping("/overLastData/page")
    public TableDataInfo<OverLastDataEntity> overLastDataList(String beginTime, String endTime, Long userId, PageQuery pageQuery) {
        return overLastDataService.selectPageOverLastDataList(beginTime, endTime, userId, pageQuery);
    }

    @ApiOperation("根据类型获取催收人列表")
    @GetMapping("/overLastData/listOverUser")
    public Result<List<SysUser>> listOverUser(@RequestParam String type) {
        if (type.equals("1")) {
            return Result.ok(overOneDataService.listOverUser());
        } else if (type.equals("2")) {
            return Result.ok(overTwoDataService.listOverUser());
        } else if (type.equals("3")) {
            return Result.ok(overLastDataService.listOverUser());
        }
        return Result.ok();
    }

    @ApiOperation("导出s0督查数据")
    @Log(title = "报表统计", businessType = BusinessType.EXPORT)
    @PostMapping("/overOneData/export")
    public void oneExport(String beginTime, String endTime, String userId, HttpServletResponse response) {
        List<OverOneDataEntity> list = overOneDataService.selectList(beginTime, endTime, userId);
        List<OneOverDataExportVo> listVo = BeanUtil.copyToList(list, OneOverDataExportVo.class);

        ExcelUtil.exportExcel(listVo, "S0监督数据报表", OneOverDataExportVo.class, response);
    }

    @ApiOperation("导出s1督查数据")
    @Log(title = "报表统计", businessType = BusinessType.EXPORT)
    @PostMapping("/overTwoData/export")
    public void twoExport(String beginTime, String endTime, String userId, HttpServletResponse response) {
        List<OverTwoDataEntity> list = overTwoDataService.selectList(beginTime, endTime, userId);
        List<TwoOverDataExportVo> listVo = BeanUtil.copyToList(list, TwoOverDataExportVo.class);

        ExcelUtil.exportExcel(listVo, "S0监督数据报表", TwoOverDataExportVo.class, response);
    }

    @ApiOperation("导出s2,S3督查数据")
    @Log(title = "报表统计", businessType = BusinessType.EXPORT)
    @PostMapping("/overLastData/export")
    public void lastExport(String beginTime, String endTime, String userId, HttpServletResponse response) {
        List<OverLastDataEntity> list = overLastDataService.selectList(beginTime, endTime, userId);
        List<LastOverDataExportVo> listVo = BeanUtil.copyToList(list, LastOverDataExportVo.class);

        ExcelUtil.exportExcel(listVo, "S0监督数据报表", LastOverDataExportVo.class, response);
    }
}
